package com.infore.common.utils;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

/**
 * 流程状态转换器
 * @author shaolay
 * @date 2022年07月01日 14:02
 */
public class StatusConverter implements Converter<String> {
    @Override
    public Class<?> supportJavaTypeKey() {
        //对象属性类型
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        //CellData属性类型
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadConverterContext<?> context) throws Exception {
        //CellData转对象属性
        String cellStr = context.getReadCellData().getStringValue();
        if (StrUtil.isEmpty(cellStr)) return null;
        if ("进行中".equals(cellStr)) {
            return "conduct";
        } else if ("已关闭".equals(cellStr)) {
            return "closed";
        } else if ("未开始".equals(cellStr)) {
            return "notStarted";
        } else if ("已到期".equals(cellStr)) {
            return "expired";
        } else {
            return null;
        }
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) throws Exception {
        //对象属性转CellData
        String cellValue = context.getValue();
        if (cellValue == null) {
            return new WriteCellData<>("");
        }
        if ("conduct".equals(cellValue)) {
            return new WriteCellData<>("进行中");
        } else if ("closed".equals(cellValue)) {
            return new WriteCellData<>("已关闭");
        } else if ("notStarted".equals(cellValue)) {
            return new WriteCellData<>("未开始");
        }else if ("expired".equals(cellValue)) {
            return new WriteCellData<>("已到期");
        } else {
            return new WriteCellData<>("");
        }
    }

}
